import { createApp, reactive, watch, ref } from "vue";

// app.config.errorHandler = errorHandler;

export let componentLib = reactive({
  lib: createApp({
    name: "ComponentLib",
    props: "components",
    data() {
      return {};
    },
    watch: {
      componentsLib: {
        handler(fresh, old) {
          console.error("[bridge][watch][componentsLib]", fresh, old);
        },
        deep: true,
        immediate: true
      }
    },
    created() {
      console.error("[bridge][created]", this);
    },
    mounted() {
      this.componentsLib = { b: 4 };
    }
  }),
  updateTime: Date.now(), //组件库更新时间
  addComponent: function (components) {
    console.log(this);
    let vppComponent = componentLib.lib.component;
    if (components && typeof components.name == "string") {
      vppComponent(components.name, components);
    } else {
      for (const k in components) {
        let cmpt = components[k];
        cmpt && vppComponent(cmpt.name, cmpt);
      }
    }
    componentLib.updateTime = Date.now();
  },
  vmCache: {} //组件缓存对象
});

watch(
  () => componentLib,
  function (fresh, old) {
    // console.error("[watch][componentLib]", fresh, old);
  },
  {
    deep: true,
    immediate: true
  }
);

console.error("[暴露到window]", "window.lib = componentLib");
window.lib = componentLib;
export let getComponentSetting = function (id, vmSetting, withPath) {
  if (!id || !vmSetting) {
    console.error(`函数getComponentSetting(id)参数id不能为空`);
    return null;
  }
  let index = null;
  let parent = null;
  let grandParent = null;
  let recurs = (id, obj, recursIndex, recursParent, recursGrandParent) => {
    if (!obj || id == null) {
      return;
    }
    if (obj.id == id) {
      index = recursIndex;
      parent = recursParent;
      grandParent = recursGrandParent;
      return obj;
    }
    let children = obj.children;
    for (const i in children) {
      let hit = recurs(id, children[i], i, children, obj); //TODO 尾递归优化
      // console.log("[obj.children]", i, children, obj);
      if (hit) {
        return hit;
      }
    }
  };
  let setting = recurs(id, vmSetting); //优先设计模式
  return withPath ? { setting: setting, index, parent, grandParent } : setting;
};
export let getComponentVm = function (id) {
  id == null && console.error(`函数getComponentVm(id)参数id不能为空`);
  componentLib.vmCache = componentLib.vmCache || {};
  return componentLib.vmCache[id];
};
export let cacheVm = function (cmpt) {
  // console.error("[cacheVm]", cmpt.setting);
  if (!cmpt || !cmpt.setting || cmpt.setting.id == null) {
    return;
  }
  componentLib.vmCache = componentLib.vmCache || {};
  let id = cmpt.setting.id;
  if (componentLib.vmCache[id]) {
    //如果组件配置项id重复,补充组件uid
    id = id + "_uid_" + cmpt._.uid;
    console.warn("[组件配置项id重复][componentLib.vmCache[id] -> cmpt]", componentLib.vmCache[id], cmpt);
  }
  componentLib.vmCache[id] = cmpt;
};
export let uncacheVm = function (cmpt) {
  // console.error("[uncacheVm]", cmpt.setting);
  if (!componentLib.vmCache || !cmpt || !cmpt.setting) {
    return;
  }
  let id = cmpt.setting.id;
  delete componentLib.vmCache[id]; //删除组件配置项id对应缓存

  let idUid = id + "_uid_" + cmpt._.uid;
  delete componentLib.vmCache[idUid]; //删除组件配置项id重复项对应缓存
};

export let getComponentsByType = function (type) {};

export let component = componentLib.lib.component;
export let addComponent = componentLib.addComponent;
